package com.yr.ryjk.user;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.validation.Valid;

import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yr.ryjk.common.ErrorType;
import com.yr.ryjk.interceptor.authority.For;
import com.yr.ryjk.interceptor.authority.UserType;
import com.yr.ryjk.redis.JedisUtil;
import com.yr.ryjk.user.entity.UserRegisterModel;
import com.yr.ryjk.user.service.IUserService;
import com.yr.ryjk.util.JSONUtil;
/**
 * 用户Controller
 * @author Rick
 *
 */
@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private IUserService userService;
	
	/**
	 * app用户注册,不拦截
	 * @param user
	 * @param session
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@RequestMapping(value="/register",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> register(@ModelAttribute @Valid UserRegisterModel user) throws IllegalAccessException, InvocationTargetException{
		String checkcodeInRedis=JedisUtil.getCheckcodeByPhone(user.getPhone());
		if(checkcodeInRedis==null){
			return JSONUtil.getJSON(ErrorType.CHECKCODE_NOT_EXSIT_ERROR);
		}else if(!checkcodeInRedis.equals(user.getCheckcode())){
			return JSONUtil.getJSON(ErrorType.CHECKCODE_ERROR);
		}else {
			return userService.addRegister(user);
		}
	}
	
	/**
	 * 根据id获取用户信息
	 * @param id
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/get",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUserInfo(@RequestParam("id")String id){
		return JSONUtil.getJSON(ErrorType.SUCCESS,userService.getUserById(id));
	}
	
	/**
	 * 后台页面分页获取数据接口
	 * @param pageNo
	 * @param pageSize
	 * @param name 查询条件
	 * @param phone 查询条件
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/list",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> list(@RequestParam(value="pageNo",defaultValue="1") int pageNo,
			@RequestParam(value="pageSize",defaultValue="10") int pageSize,
			String name, String phone){
		return JSONUtil.getJSON(ErrorType.SUCCESS,userService.getListOfPage(pageNo,pageSize,name,phone));
	}
	
	/**
	 * app用户登录,不拦截
	 * @param phone 手机号
	 * @param password 密码
	 * @return
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> login(@RequestParam("phone") @NotBlank String phone,
			@RequestParam("password") @NotBlank String password,
			@RequestParam("jpushKey") @NotBlank String jpushKey) throws IllegalAccessException, InvocationTargetException {
		return userService.updatelogin(phone,password,jpushKey);
	}
	
	/**
	 * app用户重置密码,不拦截
	 * @param phone 手机号
	 * @param newPassword 
	 * @param checkcode
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/resetPassword",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> resetPassword(@RequestParam("phone") @NotBlank String phone,
			@RequestParam("newPassword") @NotBlank String newPassword,
			@RequestParam("checkcode") @NotBlank String checkcode) {
		//1.检验验证码
		String checkcodeInRedis=JedisUtil.getCheckcodeByPhone(phone);
		if(checkcodeInRedis==null){
			return JSONUtil.getJSON(ErrorType.CHECKCODE_NOT_EXSIT_ERROR);
		}else {
			//2.更新密码
			return userService.modifyResetPassword(phone,newPassword);
		}
	}
	
	/**
	 * app用户修改密码
	 * @param oldPassword
	 * @param newPassword 
	 * @param token 令牌
	 * @return
	 */
	@For(user=UserType.APP_USER)
	@RequestMapping(value="/modifyPassword",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> modifyPassword(@RequestParam("oldPassword") @NotBlank String oldPassword,
			@RequestParam("newPassword") @NotBlank String newPassword,
			@RequestParam("token") @NotBlank String token){
		return userService.modifyPassword(oldPassword,newPassword,token);
	}
	
	/**
	 * 后台删除用户
	 * @param id 用户id
	 * @return
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(@RequestParam("id") @NotBlank String id){
		userService.deleteById(id);
		return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	/**
	 * 获取app用户的统计信息
	 */
	@For(user=UserType.WEBSERVER_USER)
	@RequestMapping(value="/getMes",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getMes(@RequestParam("id") @NotBlank String id){
		return  userService.getMesById(id);
	}
}